<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>E4X Firefox</title>
<style>
	.failed{background:red}
    .passed{background:yellow}
    .head{background:#666;color:#FFF}
    .special{background:#CCC}
	#log table{width:100%;}
	#log tbody tr:hover{background:grey;color:white}
</style>
</head>
<body>
<script type="text/javascript">
	window.onerror = function ()
	{
		console.log(arguments[2] + ": " + arguments[0]);
	};
</script>
<input type="button" value="Run Tests" onclick="run_tests()"/>
<div style="width:90%;height:100%;margin:10px auto;overflow:sroll;" id="log">
    <table>
        <thead>
            <tr class="head">
                <th>Group</th>
                <th>Message</th>
                <th>Time</th>
            </tr>
        </thead>
    </table>
</div>
<input type="button" value="Run Tests" onclick="run_tests()"/>
<script type="text/javascript">
	
var head = document.getElementsByTagName("head")[0],
	log = document.getElementById("log"),
	entries = [],
	EndTest = function(){},
	supportsXML = !!window.XML,
	notIE = navigator.userAgent.toLowerCase().indexOf('msie') == -1;

EndTest.prototype = new Error();

function run_tests ()
{
	var script = document.createElement("script"),
		script2 = document.createElement("script");
	
	entries = [];
	if (notIE)
	{
		script.onload = function()
		{
			script2.src = "xbe4x_tests.js?"+(+new Date);
			head.appendChild(script2);
		};
		script.src = (supportsXML ? "xbe4x-native.js?" : "xbe4x.js?")+(+new Date);
		head.appendChild(script);
	}
	else
	{
		script.src = "xbe4x.js?"+(+new Date);
		head.appendChild(script);
		script2.src = "xbe4x_tests.js?"+(+new Date);
		head.appendChild(script2);
	}
	
}

function test_run (tests)
{
	if (tests.setup)
		tests.setup();
	
	var s, n, p, result, startf, finish, passed = 0, failed = 0, name, tests_ran = 0, start = new Date();
	
	for (p in tests)
	{
		if (p.indexOf('test_') == 0)
		{
			name = p.substr(5).replace(/_/g, " ");
			startf = new Date();
			try{
				result = !!tests[p]() ? "PASS" : "FAIL";
			}
			catch(e)
			{
				console.log(p+": "+e.message);
				result = "FAIL";
				if (e instanceof EndTest)
				{
					break;
				}
			}
			finish = new Date() - startf;
			
			if (result == "PASS")
			{
				++passed;
				add_to_log(name, "PASS", finish + 'ms', "passed");
			}
			else
			{
				++failed;
				add_to_log(name, "FAIL", finish + 'ms', "failed");
			}
			++tests_ran;
		}
	}
	
	add_to_log("End Testing", "Total time", (new Date() - start) + 'ms', "special");
	add_to_log("&nbsp;", "Number of tests ran", tests_ran, "");
	add_to_log("&nbsp;", "Number of tests passed", passed, "passed");
	add_to_log("&nbsp;", "Number of tests failed", failed, "failed");
	
	update_log();
	s = head.getElementsByTagName("script");
	for (p = 0, n = s.length; p < n; ++p)
		head.removeChild(s[0]);
}

function add_to_log (g,m,t,c)
{
	entries[entries.length] = [g,m,t,c];
}

function update_log ()
{
	var r = '', args = entries, arg, i = 0, l = args.length;
	
	for (; i < l; ++i)
	{
		arg = args[i];
		r += '<tr class="' + arg[3] + '"><td>' + arg[0] + '<\/td><td>' + arg[1] + '<\/td><td align="right">' + arg[2] + '<\/td><\/tr>';
	}
	
	log.innerHTML = '<table style="width:100%"><thead><tr class="head"><th>Group</th><th>Message</th><th>Time</th></tr></thead><tbody>' + r + '</tbody></table>';
}

</script>
</body>
</html>
